/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boxin.ims.modules.momarketing.entity.GuestBook;
import com.boxin.ims.modules.momarketing.service.GuestBookService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 留言Controller
 * @author Jakemanse
 * @version 2013-05-28
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/mom/guestBook")
public class GuestBookFrontController extends BaseController {

	@Autowired
	private GuestBookService guestBookService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	
	@RequestMapping(value = "me")
	public String list(GuestBook guestBook, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String uid = request.getParameter("uid");
		String oid = request.getParameter("oid");
		String id = request.getParameter("id");
		
		
		User user = UserUtils.getUserById(Long.parseLong(uid));
		if(!user.isAdmin()){
			guestBook.setUser(user);
		}
		
		WechatMember member = wechatMemberService.getByWechatMemberId(user, oid);
		if(member != null){
			model.addAttribute("userName", member.getName());
			model.addAttribute("mobile", member.getMobile());
		}
		
        Page<GuestBook> page = guestBookService.find(new Page<GuestBook>(request, response), guestBook); 
        model.addAttribute("page", page);
        model.addAttribute("uid", uid);
        model.addAttribute("openId", oid);
        
		return "modules/wechat/front/guestbook/me";
	}
	
	
	
	@RequestMapping(value = "save")
	public String save(GuestBook guestBook, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String uid = request.getParameter("uid");
		String openId = request.getParameter("openId");
		String content = request.getParameter("content");
		String email = request.getParameter("email");
		String mobile = request.getParameter("mobile");
		String userName = request.getParameter("userName");
		
		
		
		try {
			
			GuestBook guestBook2 = new GuestBook();
			User user = UserUtils.getUserById(Long.parseLong(uid));
			guestBook2.setUser(user);
			guestBook2.setContent(content);
			guestBook2.setOpenId(openId);
			guestBook2.setUserName(userName);
			guestBook2.setStatus(GuestBook.STATUS_NORMAL);
			guestBook2.setEmail(email);
			guestBook2.setMobile(mobile);
			guestBookService.save(guestBook2);
			
			
			
			
	        Page<GuestBook> page = guestBookService.find(new Page<GuestBook>(request, response), guestBook); 
	        model.addAttribute("page", page);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		 model.addAttribute("uid", uid);
	     model.addAttribute("openId", "");
		
		return "modules/wechat/front/guestbook/me";
	}
	
	
	


}
